package com.ecust.ml.libsvm;

import java.io.IOException;

import libsvm.svm_model;
import libsvm.svm_node;
import Jama.Matrix;

import com.ecust.ml.cardriving.ivl.Feature;
import com.ecust.ml.cardriving.ivl.FeatureExpectation;
import com.ecust.ml.cardriving.ivl.Weight;
import com.fly.lib.util.JsonUtil;

public class Main {
	
	static String[] argv = new String[]{"/Users/pleasebugme/Desktop/model"};

	public static FeatureExpectation getFeatureExpectation(String trainDataFileName) throws IOException  {
		double[][] featureExpectationArray = new double[Feature.FEATURE_LENGTH][1];
		argv = new String[]{trainDataFileName};
		
		svm_train train = new svm_train();
		svm_model model = train.run(argv);
		svm_node[][] modelSVs = model.SV;
		double[][] modelCoefs = model.sv_coef;
		for(int i=1; i<modelSVs.length; i++) {
			for(int j=0; j<modelSVs[i].length; j++) {
				featureExpectationArray[j][0] += -modelCoefs[0][i] * modelSVs[i][j].value;
			}
		}
		
		return new FeatureExpectation(new Matrix(featureExpectationArray));
	}
	
	public static void main(String[] args) throws IOException {
		//Weight weight = getWeight("/Users/pleasebugme/Desktop/model1");
		//System.out.println(JsonUtil.toJsonString(weight.getWeightMatrix().getArray()));
		 //weight = getWeight("/Users/pleasebugme/Desktop/model2");
		//System.out.println(JsonUtil.toJsonString(weight.getWeightMatrix().getArray()));
		 //weight = getWeight("/Users/pleasebugme/Desktop/model3");
		//System.out.println(JsonUtil.toJsonString(weight.getWeightMatrix().getArray()));
		FeatureExpectation fe = getFeatureExpectation("/Users/pleasebugme/Desktop/model");
		System.out.println(JsonUtil.toJsonString(fe.getValue().getArray()));
	}
}
